﻿Module Helpers
    Public Structure RenderStates
        Public BlendState As BlendState
        Public DepthStencilState As DepthStencilState
        Public RasterizerState As RasterizerState
        Public SamplerState As SamplerState
    End Structure

    Public Class GameItems
        Private Game As Game
        Private mCameraSpeedMax As Single
        Private mCameraSpeedMin As Single
        Private mCameraSpeedIncrements As Single
        Private mCameraSpeedLocation As Single
        Private mLevelData() As CrossClasses.LevelData
        Private mSpeedMultiplier As Single
        Private mUnitSize As Single
        Private mState As RenderStates
        Public RenderTarget As RenderTarget2D


        Public RemainingJumps As Byte
        Public ScoreIncrease As Byte
        Public Level As Byte
        Public LevelEnded As Boolean
        Public Direction As Integer
        Public Speed As Single
        Public MoveSpeed As Single
        Public TimedOut As Boolean
        Public ReadOnly Property CameraSpeedIncrement As Single
            Get
                Return mCameraSpeedLocation
            End Get
        End Property
        Public Sub SpeedCamera()
            mCameraSpeedLocation += mCameraSpeedIncrements
            If (mCameraSpeedLocation > mCameraSpeedMax) Then
                mCameraSpeedLocation = mCameraSpeedMax
            End If
        End Sub
        Public Sub ReduceCamera()
            If (mCameraSpeedLocation = 0) Then
                Return
            End If
            If (mCameraSpeedLocation > 0) Then
                mCameraSpeedLocation -= mCameraSpeedIncrements
                If (mCameraSpeedLocation < 0) Then
                    mCameraSpeedLocation = 0
                End If
            End If
            If (mCameraSpeedLocation < 0) Then
                mCameraSpeedLocation += mCameraSpeedIncrements
                If (mCameraSpeedLocation > 0) Then
                    mCameraSpeedLocation = 0
                End If
            End If
        End Sub
        Public Sub SlowCamera()
            mCameraSpeedLocation -= mCameraSpeedIncrements
            If (mCameraSpeedLocation < -mCameraSpeedMin) Then
                mCameraSpeedLocation = -mCameraSpeedMin
            End If
        End Sub
        Public Sub BottomCamera()
            mCameraSpeedLocation = -mCameraSpeedMin
        End Sub
        Public Property UnitSize As Single
            Get
                Return mUnitSize
            End Get
            Set(value As Single)
                mUnitSize = value
                mCameraSpeedMax = CSng(mUnitSize * 1.2)
                mCameraSpeedMin = CSng(mUnitSize * 0.8)
            End Set
        End Property
        ' New Class
        Public Property SpeedMultiplier As Single
            Get
                If (LevelEnded) Then
                    Return 0
                Else
                    Return mSpeedMultiplier
                End If
            End Get
            Set(value As Single)
                mSpeedMultiplier = value
            End Set
        End Property

        Public Sub New(GameItem As Game)
            Game = GameItem
            UnitSize = 1.0
            Speed = CSng(mUnitSize / 2.5)
            MoveSpeed = CSng(mUnitSize / 15.0)
            mCameraSpeedIncrements = CSng(mUnitSize / 20.0)
            SpeedMultiplier = 1.0
            RemainingJumps = 0
            ScoreIncrease = 3
            Level = 0
            LevelEnded = False
            TimedOut = False
            Direction = 1
            ' Load the levels from the XML File
            mLevelData = Game.Content.Load(Of CrossClasses.LevelData())("LevelInfo")
            If (mLevelData.Count <= 0) Then
                Game.Exit()
                Return
            End If
            ' Set up the render target.
            Dim pp As PresentationParameters = Game.GraphicsDevice.PresentationParameters
            RenderTarget = New RenderTarget2D(Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, False, Game.GraphicsDevice.DisplayMode.Format, pp.DepthStencilFormat)
        End Sub
        Public Sub SetRenderTarget(ByVal blnHow As Boolean)
            'If (blnHow) Then
            '    Game.GraphicsDevice.SetRenderTarget(RenderTarget)
            'Else
            '    Game.GraphicsDevice.SetRenderTarget(Nothing)
            '    ' Draw the texture to the screen (including any disortions), make sure we record the render states because the spritebatch will reset the depth buffer
            '    RecordRenderState()
            '    With (CType(Game, Game1))
            '        .spriteBatch.Begin()
            '        .spriteBatch.Draw(RenderTarget, New Vector2(0, 0), Color.White)
            '        .spriteBatch.End()
            '    End With
            '    RestoreRenderState()
            'End If
        End Sub

        ' ////////////////////////////////////////////////////////////////////////
        ' Gets the time from the leveldata object of the current level
        Public ReadOnly Property LevelTime As Integer
            Get
                Return mLevelData(Level).Time
            End Get
        End Property
        Public ReadOnly Property Jumps As Byte
            Get
                Return mLevelData(Level).Jumps
            End Get
        End Property
        Public ReadOnly Property FileName As String
            Get
                Return mLevelData(Level).Level
            End Get
        End Property
        Public ReadOnly Property LevelData As CrossClasses.LevelData
            Get
                Return mLevelData(Level)
            End Get
        End Property
    End Class

    Public Sub RecordRenderState(ByRef States As RenderStates, ByRef GraphicsDevice As GraphicsDevice)
        States.BlendState = GraphicsDevice.BlendState
        States.DepthStencilState = GraphicsDevice.DepthStencilState
        States.RasterizerState = GraphicsDevice.RasterizerState
        States.SamplerState = GraphicsDevice.SamplerStates(0)
    End Sub
    Public Sub RestoreRenderState(ByRef States As RenderStates, ByRef GraphicsDevice As GraphicsDevice)
        GraphicsDevice.BlendState = States.BlendState
        GraphicsDevice.DepthStencilState = States.DepthStencilState
        GraphicsDevice.RasterizerState = States.RasterizerState
        GraphicsDevice.SamplerStates(0) = States.SamplerState
    End Sub
End Module
